/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package editor;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.text.DefaultEditorKit;

/**
 *
 * @author RoderickNestor
 */
public class MenuToolBar {

    Editor editor;
    private final Action cutAction = new DefaultEditorKit.CutAction();
    private final Action copyAction = new DefaultEditorKit.CopyAction();
    private final Action pasteAction = new DefaultEditorKit.PasteAction();


    private Properties config_name = new Properties();

    public MenuToolBar(Editor editor) {
        this.editor = editor;
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("editor/bundle/MenuBundle_es_PE.properties");
        try {
            config_name.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public JMenuBar getMenuBar() {
        //locale = new Locale("es", "PE");
        //label = ResourceBundle.getBundle("MenuBundle", locale);

        JMenuBar menuBar = new JMenuBar();
        JMenu menuFile = new JMenu(config_name.getProperty("menu_file"));
        JMenu menuEditor = new JMenu(config_name.getProperty("menu_editor"));
        JMenu menuGenerate = new JMenu(config_name.getProperty("menu_generate"));
        JMenu menuAbout = new JMenu(config_name.getProperty("menu_about"));

        menuBar.add(menuFile);
        menuBar.add(menuEditor);
        menuBar.add(menuGenerate);
        menuBar.add(menuAbout);

        //menu file
        JMenuItem newProjectItem = createMenuItem("menu_file_item_new", "images/folder-new16.png");
        JMenuItem openProjectItem = createMenuItem("menu_file_item_open", "images/document-open16.png");
        JMenu exampleItem = new JMenu("Example");
        JMenuItem closeProjectItem = createMenuItem("menu_file_item_close");
        JMenuItem saveItem = createMenuItem("menu_file_item_save", "images/media-floppy16.png");
        JMenuItem exitItem = createMenuItem("menu_file_item_exit");

        newProjectItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionNewProject();
            }
        });
        openProjectItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionOpenProject();
            }
        });
        closeProjectItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionCloseProject();
            }
        });

        saveItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionSave();
            }
        });

        exitItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        example(exampleItem);

        menuFile.add(newProjectItem);
        menuFile.add(openProjectItem);
        menuFile.add(exampleItem);
        menuFile.add(closeProjectItem);
        menuFile.add(saveItem);
        menuFile.add(exitItem);

        //menu edit
        JMenuItem cutItem = createMenuItem("menu_editor_item_cut");
        JMenuItem copyItem = createMenuItem("menu_editor_item_copy");
        JMenuItem pasteItem = createMenuItem("menu_editor_item_paste");

        cutItem.addActionListener(cutAction);
        copyItem.addActionListener(copyAction);
        pasteItem.addActionListener(pasteAction);

        menuEditor.add(cutItem);
        menuEditor.add(copyItem);
        menuEditor.add(pasteItem);

        JMenuItem generateItem = createMenuItem("menu_generate", "images/applications-system16.png");

        generateItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionGenerate();
            }
        });

        menuGenerate.add(generateItem);
        menuGenerate.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionGenerate();
            }
        });

        JMenuItem aboutItem = createMenuItem("menu_about_item_about");
        aboutItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                DlgAbout dlg = new DlgAbout(editor, true);
                dlg.setLocationRelativeTo(null);
                dlg.setVisible(true);
            }
        });
        menuAbout.add(aboutItem);

        return menuBar;
    }

    public JToolBar getToolBar() {
        JToolBar tool = new JToolBar();
        JButton btnNew = createToolBarButton("menu_file_item_new","images/folder-new32.png"); 
        JButton btnOpen = createToolBarButton("menu_file_item_open","images/document-open32.png");
        JButton btnSave = createToolBarButton("menu_file_item_save","images/media-floppy32.png");
        JButton btnGenerate = createToolBarButton("menu_generate_item_generate","images/applications-system32.png");
        
        btnNew.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionNewProject();
            }
        });
        

        btnOpen.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionOpenProject();
            }
        });

        
        btnSave.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionSave();
            }
        });

        
        
        btnGenerate.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionGenerate();
            }
        });

        tool.add(btnNew);
        tool.add(btnOpen);
        tool.add(btnSave);
        tool.addSeparator();
        tool.add(btnGenerate);

        return tool;
        //----------------------------------------------------------------------

    }

    protected final ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = MenuToolBar.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    JMenuItem createMenuItem(String name) {
        JMenuItem menuItem = new JMenuItem(config_name.getProperty(name));
        return menuItem;
    }

    JMenuItem createMenuItem(String name, String img_url) {
        JMenuItem menuItem = new JMenuItem(config_name.getProperty(name));
        menuItem.setIcon(createImageIcon(img_url));
        return menuItem;
    }

    JButton createToolBarButton(String name, String img_url) {
        JButton btn = new JButton(createImageIcon(img_url));
        btn.setToolTipText(config_name.getProperty(name));
        return btn;
    }

    private void example(JMenu example) {
        JMenu basic = new JMenu("01. Basic");
        JMenuItem simple = new JMenuItem("Simple");
        simple.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionOpenExample("basic" + File.separator + "Simple");
            }
        });
        JMenuItem view = new JMenuItem("View");
        simple.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editor.actionOpenExample("basic" + File.separator + "View");
            }
        });
        basic.add(simple);
        basic.add(view);
        example.add(basic);
    }

}
